import apiClient from '../apiClient';
import {
  GetCreatedMcpListRes,
  GetCreatedMcpListReq,
  PaginationResponse,
  GetCreatedMcpListItem,
  RunToolsParams,
  CreateMcpParams,
  GetMcpServersListItem,
  GetCreatedMcpServersItem,
  ApprovedRes,
  ApprovedReq
} from '@/common/types';

export enum McpApi {
  GetCategoriesList = '/openapi/v2/mcp/categories',
  GetTemplateList = '/openapi/v2/mcp/template/list',
  GetMcpList = '/openapi/v2/mcp/list',
  GetDetail = '/openapi/v2/mcp/detail',
  GetServersList = '/openapi/v2/mcp/servers/list',
  CreateMcp = '/openapi/v2/mcp/create',
  UpdateMcp = '/openapi/v2/mcp/update',
  DeleteMcp = '/openapi/v2/mcp/delete',
  GetToolsList = '/openapi/v2/mcp/tools/list',
  RunTools = '/openapi/v2/mcp/tools/run',
  CreateServers = '/openapi/v2/mcp/servers/create',
  UpdateServers = '/openapi/v2/mcp/servers/update',
  DeleteServers = '/openapi/v2/mcp/servers/delete',
  Approved = '/openapi/v2/apps/approved/list',
  CreateCategories = '/openapi/v2/mcp/categories/create',
  DeleteCategories = '/openapi/v2/mcp/categories/delete',
}

const getApproved = (data: ApprovedReq) =>
  apiClient.get<ApprovedRes>({ url: McpApi.Approved, params: data });


/* 
*获取所有MCP配置列表, 由模板创建的要还原回模板的格式
*/
const getMcpList = (params: GetCreatedMcpListRes) =>
  apiClient.get<PaginationResponse<GetCreatedMcpListItem>>({ url: McpApi.GetMcpList, params });


/*
*获取所有MCP分类
*/
const getCategoriesList = () => {
  return apiClient.get({ url: McpApi.GetCategoriesList });
};

/*
*新增MCP分类
*/
const createCategories = (nameCN: string) => {
  return apiClient.post({ url: McpApi.CreateCategories, data: { nameCN } });
};

/*
* 删除MCP分类
*/
const deleteCategories = (id: string) => {
  let url = `${McpApi.DeleteCategories}`;
  if (id) {
    url += `?id=${id}`;
  }
  return apiClient.post({ url });
};

/*
*获取所有MCP模板列表
*/
const getTemplateList = (params: GetCreatedMcpListRes) => {
  return apiClient.get({ url: McpApi.GetTemplateList, params });
};

/*
*获取MCP服务列表
*/
const getServersList = (params: GetCreatedMcpListRes) => {
  return apiClient.get({ url: McpApi.GetServersList, params });
};

/*
* 测试工具调用
*/
const runTools = (data: RunToolsParams) => {
  return apiClient.post({ url: McpApi.RunTools, data });
};

/*
* 创建新的MCP, 支持自定义创建和通过模板创建
*/
const updateMcp = (data: CreateMcpParams) => {
  return apiClient.post({ url: McpApi.UpdateMcp, data });
};
/*
* 更新指定MCP配置
*/
const createMcp = (data: CreateMcpParams) => {
  return apiClient.post({ url: McpApi.CreateMcp, data });
};

/*
*获取工具列表
*/
const getToolsList = (templateId?: string, variables?: object, url?: string, headerAuth?: object) => {
  let params = {
    templateId,
    variables,
    url,
    headerAuth
  }
  return apiClient.post({ url: McpApi.GetToolsList, data: params });
};

/*
*删除指定MCP配置
*/
const deleteMcp = (id: string) => {
  let url = `${McpApi.DeleteMcp}`;
  if (id) {
    url += `?id=${id}`;
  }
  return apiClient.post({ url });
};

/*
*删除MCP服务
*/
const deleteServers = (ids: string[]) => {
  let url = `${McpApi.DeleteServers}`;
  let params = {
    ids
  }
  return apiClient.post({ url, data: params });
};

/*
* 更新MCP服务
*/
const updateServers = (data: GetCreatedMcpServersItem) => {
  let url = `${McpApi.UpdateServers}`;
  return apiClient.post({ url, data });
};

/*
* 创建MCP服务
*/
const createServers = (data: GetCreatedMcpServersItem) => {
  let url = `${McpApi.CreateServers}`;
  return apiClient.post({ url, data });
};

/*
* 获取MCP配置详情, 由模板创建的要还原回模板的格式
*/
const getDetail = (id: string) => {
  let url = `${McpApi.GetDetail}`;
  if (id) {
    url += `?id=${id}`;
  }
  return apiClient.get({ url });
};
export default {
  getMcpList,
  getCategoriesList,
  getTemplateList,
  runTools,
  createMcp,
  getToolsList,
  deleteMcp,
  getDetail,
  updateMcp,
  getServersList,
  deleteServers,
  updateServers,
  getApproved,
  createServers,
  createCategories,
  deleteCategories
};
